package org.jeecg.modules.demo.com.kunpeng.controller;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.com.kunpeng.entity.CommodityPrice;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.CommodityPriceQueryListDTO;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.CommodityPurchaseOrSaleDTO;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.com.kunpeng.service.ICommodityPriceService;
import org.jeecg.modules.demo.com.kunpeng.vo.CommodityPriceQueryListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 商品价目表
 * @Author: jeecg-boot
 * @Date:   2023-01-11
 * @Version: V1.0
 */
@Api(tags="商品价目表")
@RestController
@RequestMapping("/com.kunpeng/commodityPrice")
@Slf4j
public class CommodityPriceController extends JeecgController<CommodityPrice, ICommodityPriceService> {
	@Autowired
	private ICommodityPriceService commodityPriceService;
	
	/**
	 * 分页列表查询
	 *
	 * @param commodityPrice
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "商品价目表-分页列表查询")
	@ApiOperation(value="商品价目表-分页列表查询", notes="商品价目表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CommodityPrice>> queryPageList(CommodityPrice commodityPrice,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CommodityPrice> queryWrapper = QueryGenerator.initQueryWrapper(commodityPrice, req.getParameterMap());
		Page<CommodityPrice> page = new Page<CommodityPrice>(pageNo, pageSize);
		IPage<CommodityPrice> pageList = commodityPriceService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 * 添加
	 * @param commodityPrice
	 * @return
	 */
	@AutoLog(value = "商品价目表-添加")
	@ApiOperation(value="商品价目表-添加", notes="商品价目表-添加")
	//@RequiresPermissions("com.kunpeng:commodity_price:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CommodityPrice commodityPrice) {
		commodityPriceService.insert(commodityPrice);
		return Result.OK("添加成功！");
	}
	
	/**
	 * 编辑
	 * @param commodityPrice
	 * @return
	 */
	@AutoLog(value = "商品价目表-编辑")
	@ApiOperation(value="商品价目表-编辑", notes="商品价目表-编辑")
	//@RequiresPermissions("com.kunpeng:commodity_price:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CommodityPrice commodityPrice) {
		commodityPrice.setIsDelete("0");
		commodityPriceService.updateById(commodityPrice);
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商品价目表-通过id删除")
	@ApiOperation(value="商品价目表-通过id删除", notes="商品价目表-通过id删除")
	//@RequiresPermissions("com.kunpeng:commodity_price:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		commodityPriceService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "商品价目表-批量删除")
	@ApiOperation(value="商品价目表-批量删除", notes="商品价目表-批量删除")
	//@RequiresPermissions("com.kunpeng:commodity_price:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.commodityPriceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "商品价目表-通过id查询")
	@ApiOperation(value="商品价目表-通过id查询", notes="商品价目表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CommodityPrice> queryById(@RequestParam(name="id",required=true) String id) {
		CommodityPrice commodityPrice = commodityPriceService.getById(id);
		if(commodityPrice==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(commodityPrice);
	}

    /**
    * 导出excel
    * @param request
    * @param commodityPrice
    */
    //@RequiresPermissions("com.kunpeng:commodity_price:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CommodityPrice commodityPrice) {
        return super.exportXls(request, commodityPrice, CommodityPrice.class, "商品价目表");
    }

    /*** 通过excel导入数据
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("com.kunpeng:commodity_price:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CommodityPrice.class);
    }

	 /**
	  * 模糊查询
	  * @param commodityPriceQueryListDTO
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	 @ApiOperation(value = "商品价目表-模糊查询")
	 @PostMapping(value = "/queryListByGrade")
	 public Result<Page<CommodityPriceQueryListVO>> queryListByGrade(@RequestBody CommodityPriceQueryListDTO commodityPriceQueryListDTO,
																	 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																	 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
		 log.info("开始处理根据关键字查询项目列表请求...");
		 System.out.println("commodityPriceQueryListDTO:"+commodityPriceQueryListDTO.toString());
		 Result<Page<CommodityPriceQueryListVO>> result = new Result<Page<CommodityPriceQueryListVO>>();
		 Page<CommodityPriceQueryListVO> pageList = new Page<CommodityPriceQueryListVO>(pageNo, pageSize);
		 pageList = commodityPriceService.queryListByKeyWords(pageList, commodityPriceQueryListDTO);//通知公告消息
		 if (pageList.getRecords().size() == 0) {
			 return Result.error("没有符合条件的数据，请重新输入！");
		 }
		 log.info("查询当前页：" + pageList.getCurrent());
		 log.info("查询当前页数量：" + pageList.getSize());
		 log.info("查询结果数量：" + pageList.getRecords().size());
		 log.info("数据总数：" + pageList.getTotal());
		 result.setSuccess(true);
		 result.setResult(pageList);
		 return Result.ok(pageList);
	 }

	 /**
	  * 修改isDelete状态
	  * @param id
	  * @param isDelete
	  * @return
	  */
	 @ApiOperation(value = "商品价目表-修改isDelete状态")
	 @GetMapping(value = "/updateIsDeleteById")
	 public Result<String> updateIsDeleteById(String id,String isDelete){
	 	commodityPriceService.updateIsDelete(id,isDelete);
	 	return Result.ok("修改完成！");
	 }

	 /**
	  * 查询采购商品列表
	  */
	 @ApiOperation(value = "商品价目表-商品采购列表")
	 @PostMapping(value = "/queryPurchaseList")
	 public Result<Page<CommodityPriceQueryListVO>> queryPurchaseList(@RequestBody CommodityPurchaseOrSaleDTO commodityPurchaseOrSaleDTO,
																@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																@RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
		 log.info("开始处理根据关键字查询项目列表请求...");
		 Result<Page<CommodityPriceQueryListVO>> result = new Result<Page<CommodityPriceQueryListVO>>();
		 Page<CommodityPriceQueryListVO> pageList = new Page<CommodityPriceQueryListVO>(pageNo, pageSize);
		 pageList = commodityPriceService.queryPurchaseCommodityList(pageList, commodityPurchaseOrSaleDTO);//通知公告消息
		 if (pageList.getRecords().size() == 0) {
			 return Result.error("没有符合条件的数据，请重新输入！");
		 }
		 log.info("查询当前页：" + pageList.getCurrent());
		 log.info("查询当前页数量：" + pageList.getSize());
		 log.info("查询结果数量：" + pageList.getRecords().size());
		 log.info("数据总数：" + pageList.getTotal());
		 result.setSuccess(true);
		 result.setResult(pageList);
		 return Result.ok(pageList);
	 }

	 /**
	  * 查询销售商品列表
	  */
	 @ApiOperation(value = "商品价目表-商品销售列表")
	 @PostMapping(value = "/querySaleList")
	 public Result<Page<CommodityPriceQueryListVO>> querySaleList(@RequestBody CommodityPurchaseOrSaleDTO commodityPurchaseOrSaleDTO,
																	  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																	  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
		 log.info("开始处理根据关键字查询项目列表请求...");
		 Result<Page<CommodityPriceQueryListVO>> result = new Result<Page<CommodityPriceQueryListVO>>();
		 Page<CommodityPriceQueryListVO> pageList = new Page<CommodityPriceQueryListVO>(pageNo, pageSize);
		 pageList = commodityPriceService.querySalesCommodityList(pageList, commodityPurchaseOrSaleDTO);//通知公告消息
		 if (pageList.getRecords().size() == 0) {
			 return Result.error("没有符合条件的数据，请重新输入！");
		 }
		 log.info("查询当前页：" + pageList.getCurrent());
		 log.info("查询当前页数量：" + pageList.getSize());
		 log.info("查询结果数量：" + pageList.getRecords().size());
		 log.info("数据总数：" + pageList.getTotal());
		 result.setSuccess(true);
		 result.setResult(pageList);
		 return Result.ok(pageList);
	 }

	/**
	 * 商品编码生成器
	 */
	/*@ApiOperation(value = "商品价目表-商品编码生成器")
	@GetMapping(value = "/createCommodityCode")
	public Result<String> createCommodityCode(String prefix){
		log.info("开始生成商品编码...");
		//获取当前时间并进行格式转换
		LocalDate now = LocalDate.now();
		DateTimeFormatter dtf= DateTimeFormatter.ofPattern("yyyy-MM-dd"); //定义时间格式
		String dateStr = now.format(dtf); //转换成String类型
		String[] split = dateStr.split("-");
		//判断是否传输正确
		if(prefix.equals("SP")){
			String commodityCodePrefix = "SP" + split[0]+split[1] + split[2];
			System.out.println("商品编码前缀："+commodityCodePrefix);

			Integer commodityCodeSuffix = commodityPriceService.commodityPriceListByCode(commodityCodePrefix);

			System.out.println("commodityCodeSuffix:"+commodityCodeSuffix);
			if(commodityCodeSuffix<=9){
				commodityCodePrefix+="00"+commodityCodeSuffix;
			}else if(commodityCodeSuffix>9 && commodityCodeSuffix<=99){
				commodityCodePrefix+="0"+commodityCodeSuffix;
			}else if(commodityCodeSuffix>99){
				commodityCodePrefix+=commodityCodeSuffix;
			}
			return Result.OK(commodityCodePrefix);
		}
		return Result.error("商品编码生成出错，请联系管理员！");
	}*/
}
